package com.googlecode.iiddefecttracker.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.jdbc.object.MappingSqlQuery;

import com.googlecode.iiddefecttracker.domain.Project;
import com.googlecode.iiddefecttracker.domain.ProjectBeanImpl;

/**
 * Base implementation of {@link ProjectDao} that uses Spring JDBC services.
 */
public class ProjectDaoSpringJDBC extends SimpleJdbcDaoSupport implements
        ProjectDao {
    
    private class ProjectsAllQuery extends MappingSqlQuery {
        
        protected ProjectsAllQuery(final DataSource ds) {
            super(ds,
                    "SELECT id, name, version, description FROM projects ORDER BY id");
            compile();
        }
        
        @Override
        protected Project mapRow(final ResultSet rs, final int rowNum)
                throws SQLException {
            final Long id = new Long(rs.getLong("id"));
            final String name = rs.getString("name");
            final String version = rs.getString("version");
            final String description = rs.getString("description");
            final Project project = new ProjectBeanImpl();
            project.setId(id);
            project.setName(name);
            project.setVersion(version);
            project.setDescription(description);
            
            return project;
        }
    }
    
    private ProjectsAllQuery projectsAllQuery;
    
    @SuppressWarnings("unchecked")
    public List<Project> findAllProjects() {
        return projectsAllQuery.execute();
    }
    
    public Project getProjectById(final Long projectId) {
        
        final String sql = "select id, name, version, description FROM projects where id = ? ORDER BY id";
        
        final ParameterizedRowMapper<Project> mapper = new ParameterizedRowMapper<Project>() {
            
            public Project mapRow(final ResultSet rs, final int rowNum)
                    throws SQLException {
                
                final Long id = new Long(rs.getLong("id"));
                final String name = rs.getString("name");
                final String version = rs.getString("version");
                final String description = rs.getString("description");
                
                final Project project = new ProjectBeanImpl();
                project.setId(id);
                project.setName(name);
                project.setVersion(version);
                project.setDescription(description);
                
                return project;
            }
        };
        
        return getSimpleJdbcTemplate().queryForObject(sql, mapper,
                new Object[] { projectId });
    }
    
    public void saveProject(final Project project) {
        final Object[] queryParams = new Object[] { project.getName(),
                project.getVersion(), project.getDescription() };
        
        getSimpleJdbcTemplate()
                .update(
                        "insert into projects (name, version, description) values (?,?,?)",
                        queryParams);
    }
    
    @Override
    protected void initDao() throws Exception {
        super.initDao();
        projectsAllQuery = new ProjectsAllQuery(getDataSource());
    }
}